<?php

/* Copyright (c) 2012 Universidad Del Atlántico

  Autores
  Departamento de Admisiones y registro Academico Universidad del Atlantico
  Carlos Fuentes

  /* All rights reserved.

  /* Redistribution and use in source and binary forms, with or without
  /* modification, are permitted provided that the following conditions
  /* are met:
  /* 1. Redistributions of source code must retain the above copyright
  /*    notice, this list of conditions and the following disclaimer.
  /* 2. Redistributions in binary form must reproduce the above copyright
  /*    notice, this list of conditions and the following disclaimer in the
  /*    documentation and/or other materials provided with the distribution.
  /* 3. Neither the name of copyright holders nor the names of its
  /*    contributors may be used to endorse or promote products derived
  /*    from this software without specific prior written permission.

  /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  /* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  /* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  /* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
  /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  /* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  /* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  /* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  /* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  /* POSSIBILITY OF SUCH DAMAGE.


 */

/**
 * Description of LoggedUser
 *
 * @author UNIATLANTICO
 */
class LoggedUser {

    protected $id;
    protected $grupos = Array();
    protected $user;
    protected $visitor;
    protected $admin;

    public function __construct($id) {
        $this->setId($id);
        $db = new connection();
        $this->user = AlaniaUsuarioModel::findById($db->db, $id);
        $this->setGroups($db->db);
        $this->writeUserLog($db->db);
        $this->setIsAdmin();
        $this->setIsVisitor();
    }

    protected function setID($id) {
        $this->id = $id;
    }

    protected function setGroups(PDO $db) {
        //$firephp = FirePHP::getInstance(true);
        foreach ($this->user->fetchPerfilDependenciaUsuarioModelCollection($db) as $value) {
            //$firephp->log($value, "grupo");
            array_push($this->grupos, $value->getIdperfilDependenciaUsuario());
//            if (intval($value->getIdperfilDependenciaUsuario()) == 1) {
//                //$firephp->warn("es admin");
//                $this->setIsAdmin(true);
//                //$firephp->warn(intval($this->admin),"es admin");
//            }
        }
    }

//    protected function setIsAdmin($bool){
//        $this->admin=$bool;
//    }
    protected function setIsAdmin() {
        //$firephp=  FirePHP::getInstance(true);
        foreach ($this->grupos as $value) {
            if (intval($value) == 1) {
                
                $this->admin = true;
                //$firephp->log($this);
                break;
            } else {
                $this->admin = false;
            }
        }
        //$firephp->log($this,"afuera foreach");
        
    }

    protected function setIsVisitor() {
        if ($this->id == 7)
            $this->visitor = TRUE;
        else
            $this->visitor = false;
    }

    public function isVisitor() {
        return $this->visitor;
    }

    public function isAdmin() {
        //$firephp = FirePHP::getInstance(true);
        //$firephp->warn(intval($this->admin), "iadmin");
        return $this->admin;
    }

    public function getUser() {
        return $this->user;
    }

    protected function writeUserLog(PDO $db) {
        $log1 = new AlaniaLogModel();
        $log1->setFechaLog(date('y:m:d h:i:s'));
        $log1->setInformacion(1);
        $log1->setTipoEvento(0);
        $log1->setUsuario($this->user->getIdUsuario());
        $log1->setContenido("");
        $log1->insertIntoDatabase($db);
        $this->user->setLastSeen(date('y:m:d h:i:s'));
        $this->user->updateToDatabase($db);
    }

    public function checkPermissions(PDO $db, $info) {
        $perm = array();
        $group = "00";
        $user = "00";
        //$firephp = FirePHP::getInstance(true);

        //$firephp->warn($this->isAdmin() ? "es admin" : "no es admin");
        //$firephp->warn($this->isVisitor() ? "es vistori" : "no es vistori");
        if ($this->isAdmin()) {
            $group = "11";
        } else {
            foreach ($this->grupos as $grupo) {
                $filterg = array(PermisosInformacionGrupoModel::FIELD_INFORMACION => $info, PermisosInformacionGrupoModel::FIELD_PERFIL_DEPENDENCIA => $grupo);
                $grupos = PermisosInformacionGrupoModel::findByFilter($db, $filterg, true);
                if (count($grupos)) {
                    foreach ($grupos as $value) {

                        if ($group < $value->getFuncion()) {
                            $group = $value->getFuncion();
                        }
                    }
                } else {
                    $group = "-1";
                }
            }
        }
        $filteru = array(PermisosInformacionUsuarioModel::FIELD_INFORMACION => $info, PermisosInformacionUsuarioModel::FIELD_USUARIO => $this->id);
        $permu = PermisosInformacionUsuarioModel::findByFilter($db, $filteru, true);
        if (count($permu)) {
            foreach ($permu as $value) {
                $user = $value->getFuncion();
            }
        } else {
            $user = "-1";
        }

        if (intval($group)) {
            if (intval($group) > 0) {
                if (intval($user)) {
                    //asgiando en usuario
                    if (intval($user) > 0) {
                        return $group > $user ? $group : $user;

                        //no asignado en usuario    
                    } else {
                        return $group;
                    }
                    //negado en usuario
                } else {
                    return "00";
                }
                //no asignado en grupos
            } else {
                if (intval($user)) {
                    //asgiando en usuario
                    if (intval($user) > 0) {
                        return $user;

                        //no asignado en usuario    
                    } else {
                        return "00";
                    }
                    //negado en usuario
                } else {
                    return "00";
                }
            }
            //negado en grupos
        } else {
            if (intval($user)) {
                //asgiando en usuario
                if (intval($user) > 0) {
                    return $user;

                    //no asignado en usuario    
                } else {
                    return "00";
                }
                //negado en usuario
            } else {
                return "00";
            }
        }
    }

}

?>
